package com.novax.ex.activity.provider.handler.consumer;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.novax.ex.activity.provider.api.model.AssetChangeRequest;
import com.novax.ex.activity.provider.service.BeebUserService;
import com.novax.ex.common.constant.MqConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;

/**
 * @Description
 * @Author zhaolei
 * @Date 10/24/24 5:08 PM
 * @Version 1.0
 */
@Component
@Slf4j
public class BeebUserConsumer {
    @Resource
    private BeebUserService beebUserService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConstants.Queue.BEEB_USER, durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.BEEB_USER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Exchange.BEEB_USER_KEY)
    )
    public void exec(String message) {
        try {
            if (StringUtils.isNotBlank(message)){
                beebUserService.exec(message);
            }
        } catch (Exception e) {
            log.info(e.toString());
            log.info("消息队列丢失消息:" + message);
        }
    }


    /**
     * 用户资产解锁
     * @param message
     */
    @RabbitListener(queuesToDeclare = @Queue(value = MqConstants.Queue.SEND_BEEB_ASSETS, durable = "true"))
    public void unlock(String message) {
        try {
            if (StringUtils.isNotBlank(message)){
                AssetChangeRequest request = JSON.parseObject(message, AssetChangeRequest.class);
                beebUserService.unfrozen(request);
            }
        } catch (Exception e) {
            log.info(e.toString());
            log.info("消费-解锁异常:" + message);
        }
    }
}
